package in.animeshpathak.appic;

import in.animeshpathak.appic.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/*
 * The activity managing the settings screen.
 * Expected behaviors:
 * 1. Launches when the settings button is hit from the menu
 * 2. If cancel is hit, goes back to calling activity
 * 3. If save is hit, saves data in prefs, shows toast saying "settings saved". Stays on activity.
 * 4. If system goes into suspend, stores state into bundle.
 * 5. OnCreate, loads from pref store, but onReloadData, reloads it from bundle
 */
public class SettingsActivity extends Activity {
	
	private String apiUrl;
	private String apiUName;
	private String apiPass;
	
	
	@Override
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings_relative);
		
		//set content of textboxes based on actual settings.
		getStoredPreferenceData();
		((EditText)findViewById(R.id.apiUrlText)).setText(this.apiUrl);
		((EditText)findViewById(R.id.apiUnameText)).setText(this.apiUName);
		((EditText)findViewById(R.id.apiPassText)).setText(this.apiPass);
		
		
		//setting handlers for the pick button
		Button saveSettingsButton = (Button)findViewById(R.id.save_settings_button);
		saveSettingsButton.setOnClickListener(new OnClickListener(){
			// Implement the OnClickListener callback for the file chooser.
			public void onClick(View v) {
				// do something when the button is clicked
				Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Save Settings Button clicked!");

				//save the form to settings
				apiUrl = ((EditText)findViewById(R.id.apiUrlText)).getText().toString();
				apiUName = ((EditText)findViewById(R.id.apiUnameText)).getText().toString();
				apiPass = ((EditText)findViewById(R.id.apiPassText)).getText().toString();
				
				setStoredPreferenceData();
				
				//show toast that settings were saved
				Context context = getApplicationContext();
				CharSequence text = "Settings Saved";
				int duration = Toast.LENGTH_SHORT;

				Toast.makeText(context, text, duration).show();
				
				//go back automatically
				finish();

			}
		});

		//setting handler for Cancel button
		Button cancelSettingsButton = (Button)findViewById(R.id.cancel_settings_button);
		cancelSettingsButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//finish this activity and go back.
				finish();
			}
		});

		
	}
	
	
	/** gets data from stored preferences. Called only on onCreate */
	private void getStoredPreferenceData() {
		// Restore preferences
		SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
		this.apiUrl = settings.getString(Constants.APIURL_PREFS_KEY, "");
		Log.d(Constants.HOME_ACTIVITY_LOGTAG, "Got stored apiUrl " + this.apiUrl);

		this.apiUName = settings.getString(Constants.APIUNAME_PREFS_KEY, "");
		Log.d(Constants.HOME_ACTIVITY_LOGTAG, "Got stored apiUName " + this.apiUName);

		this.apiPass = settings.getString(Constants.APIPASS_PREFS_KEY, "");
		//Log.d(Constants.HOME_ACTIVITY_LOGTAG, "Got stored apiPass " + this.apiPass);
		//for production code
		Log.d(Constants.HOME_ACTIVITY_LOGTAG, "Got stored apiPass " + "*** ;-)");

	}
	
	/**
	 * Store api info into preferences.
	 */
	private void setStoredPreferenceData(){
		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(Constants.APIURL_PREFS_KEY, this.apiUrl);
		Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Storing apiUrl " + this.apiUrl);

		editor.putString(Constants.APIUNAME_PREFS_KEY, this.apiUName);
		Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Stored apiUName " + this.apiUName);

		editor.putString(Constants.APIPASS_PREFS_KEY, this.apiPass);
		Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Stored apiPass " + this.apiPass);

		// Commit the edits!
		editor.commit();
	}
	
	@Override
	protected void onSaveInstanceState (Bundle outState){

		super.onSaveInstanceState(outState);

		//store API URL
		String apiUrlText = ((EditText)findViewById(R.id.apiUrlText)).getText().toString();
		Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Saving API URL Text: " + apiUrlText);
		outState.putString(Constants.APIURL_FORM_KEY, apiUrlText);
		//store API UName
		String apiUnameText = ((EditText)findViewById(R.id.apiUnameText)).getText().toString();
		Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Saving API Uname: " + apiUnameText);
		outState.putString(Constants.APIUNAME_FORM_KEY,apiUnameText);		
		//store API Password
		String apiPassText = ((EditText)findViewById(R.id.apiPassText)).getText().toString();
		Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Saving API Password: " + apiPassText);
		outState.putString(Constants.APIPASS_FORM_KEY, apiPassText);

	}

	@Override
	protected void onRestoreInstanceState (Bundle savedInstanceState){

		super.onRestoreInstanceState(savedInstanceState);

		//retrieve API URL
		String apiUrlText = savedInstanceState.getString(Constants.APIURL_FORM_KEY);
		if (apiUrlText != null){
			Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Resumed. Got API URL " + apiUrlText );
			EditText e = (EditText)findViewById(R.id.apiUrlText);
			e.setText(apiUrlText);
		}else{
			Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Resumed. Got Null API URL");
		}

		//retrieve API Uname
		String apiUnameText = savedInstanceState.getString(Constants.APIUNAME_FORM_KEY);
		if (apiUnameText != null){
			Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Resumed. Got API Uname " + apiUnameText );
			EditText e = (EditText)findViewById(R.id.apiUnameText);
			e.setText(apiUnameText);
		}else{
			Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Resumed. Got Null API Uname");
		}

		//retrieve API Password
		String apiPassText = savedInstanceState.getString(Constants.APIPASS_FORM_KEY);
		if (apiPassText != null){
			Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Resumed. Got API Password " + apiPassText );
			EditText e = (EditText)findViewById(R.id.apiPassText);
			e.setText(apiPassText);
		}else{
			Log.d(Constants.SETTINGS_ACTIVITY_LOGTAG, "Resumed. Got Null API Password");
		}
	}


}
